//
//  SetupViewController.m
//  LiuHeBao
//
//  Created by zr on 2018/8/22.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "SetupViewController.h"

@interface SetupViewController ()

@property (nonatomic, weak)UIButton * backButton;
@property (nonatomic, weak)UIImageView * backImageView;
@property (nonatomic, strong)UITextField * accountTextFiled;
@property (nonatomic, weak)UITextField * verufyTextFiled;
@property (nonatomic, strong)UITextField * passwordTextFiled;
@property (nonatomic, strong)UITextField * sexTextFiled;
@property (nonatomic, strong)UITextField * briTextFiled;
@property (nonatomic, weak)UIButton * loginButton;


@end

@implementation SetupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    [self setupUI];
}


- (void)setupUI{
    
    self.view.backgroundColor = WHITE_COLOR;
    
    UIImageView * backImageView = [UIImageView new];
    
    backImageView.image = ImageNamed(@"zhuce");
    backImageView.userInteractionEnabled = YES;
    
    self.backImageView = backImageView;
    
    [self.view addSubview:self.backImageView];
    
    
    UIButton * backButton = [UIButton new];
    
    [backButton setImage:ImageNamed(@"basefanhui") forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBackButton) forControlEvents:UIControlEventTouchUpInside];
    
    self.backButton = backButton;
    
    [self.backImageView addSubview:self.backButton];
    
    
    self.accountTextFiled = [self setupTextFiledWith:@"请输入手机号"];
    [self.view addSubview:self.accountTextFiled];
    
    self.passwordTextFiled = [self setupTextFiledWith:@"密码"];
    self.passwordTextFiled.secureTextEntry = YES;
    [self.view addSubview:self.passwordTextFiled];
    
    self.sexTextFiled = [self setupTextFiledWith:@"性别"];
    [self.view addSubview:self.sexTextFiled];
    
    self.briTextFiled = [self setupTextFiledWith:@"出生年月"];
    [self.view addSubview:self.briTextFiled];
    
    
    UITextField * verufyTextFiled = [UITextField new];
    
    verufyTextFiled.borderStyle = UITextBorderStyleRoundedRect;
    verufyTextFiled.backgroundColor = WHITE_COLOR;
    verufyTextFiled.layer.borderColor = [ColorWithRGB(0xe5e5e5) CGColor];
    verufyTextFiled.layer.borderWidth = 1.0f;
    //设置光标颜色
    verufyTextFiled.tintColor = BLACK_COLOR;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x919191);//[UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    verufyTextFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"验证码" attributes:attrs];
    verufyTextFiled.textColor = BLACK_COLOR;
    verufyTextFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    verufyTextFiled.leftViewMode = UITextFieldViewModeAlways;
    verufyTextFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    //    textFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120 * SCREENH6, 25 * SCREENH6)];
    
    UIButton * verButton = [UIButton new];
    
    [verButton setTitle:@" 获取验证码 " forState:UIControlStateNormal];
    [verButton setTitleColor:ColorWithRGB(0x262626) forState:UIControlStateNormal];
    [verButton addTarget:self action:@selector(clickVerifyButton) forControlEvents:UIControlEventTouchUpInside];
    verButton.titleLabel.font = [UIFont systemFontOfSize:14 * SCREENH6];
    [rightView addSubview:verButton];
    
    [verButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(rightView);
        make.centerY.mas_equalTo(rightView);
    }];
    
    verufyTextFiled.rightView = rightView;
    verufyTextFiled.rightViewMode = UITextFieldViewModeAlways;
    
    self.verufyTextFiled = verufyTextFiled;
    
    [self.view addSubview:self.verufyTextFiled];
    
    
    UIButton * loginBurron = [UIButton new];
    
    [loginBurron setBackgroundColor:ColorWithRGB(0xDA4136)];
    [loginBurron setTitle:@"注册" forState:UIControlStateNormal];
    [loginBurron setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
    [loginBurron addTarget:self action:@selector(clickLoginButton) forControlEvents:UIControlEventTouchUpInside];
    loginBurron.titleLabel.font = [UIFont systemFontOfSize:17 * SCREENH6];
    loginBurron.layer.cornerRadius = 20 * SCREENH6;
    loginBurron.layer.masksToBounds = YES;
    
    self.loginButton = loginBurron;
    
    [self.view addSubview:self.loginButton];
    
}


- (void)viewDidLayoutSubviews{
    
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.mas_equalTo(self.view);
        make.height.mas_equalTo(260 * SCREENH6);
    }];

    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.backImageView).offset(15 * SCREENH6);
        make.top.mas_equalTo(self.backImageView).offset(30 * SCREENH6);
        make.width.height.mas_equalTo(20 * SCREENH6);
    }];
    
    [self.accountTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(25 * SCREENH6);
        make.trailing.mas_equalTo(self.view).offset(-25 * SCREENH6);
        make.height.mas_equalTo(40 * SCREENH6);
        make.top.mas_equalTo(self.backImageView.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.verufyTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.accountTextFiled);
        make.top.mas_equalTo(self.accountTextFiled.mas_bottom).offset(20 * SCREENH6);
    }];
    
    [self.passwordTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.verufyTextFiled);
        make.top.mas_equalTo(self.verufyTextFiled.mas_bottom).offset(20 * SCREENH6);
    }];
    
//    [self.sexTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.height.mas_equalTo(self.passwordTextFiled);
//        make.top.mas_equalTo(self.passwordTextFiled.mas_bottom).offset(20 * SCREENH6);
//    }];
//
//    [self.briTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.leading.trailing.height.mas_equalTo(self.sexTextFiled);
//        make.top.mas_equalTo(self.sexTextFiled.mas_bottom).offset(20 * SCREENH6);
//    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.height.mas_equalTo(self.passwordTextFiled);
        make.top.mas_equalTo(self.passwordTextFiled.mas_bottom).offset(25 * SCREENH6);
    }];
    
}


- (UITextField *)setupTextFiledWith:(NSString *)str{
    
    UITextField * textFiled = [UITextField new];
    
    textFiled.borderStyle = UITextBorderStyleRoundedRect;
    textFiled.backgroundColor = WHITE_COLOR;
    textFiled.layer.borderColor = [ColorWithRGB(0xe5e5e5) CGColor];
    textFiled.layer.borderWidth = 1.0f;
    //设置光标颜色
    textFiled.tintColor = BLACK_COLOR;
    //修改占位符文字颜色
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = ColorWithRGB(0x919191);//[UIColor lightGrayColor];
    attrs[NSFontAttributeName] = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    textFiled.attributedPlaceholder = [[NSAttributedString alloc] initWithString:str attributes:attrs];
    textFiled.textColor = BLACK_COLOR;
    textFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 22 * SCREENH6, 0)];
    //设置显示模式为永远显示(默认不显示)
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15 * SCREENH6];
    //    textFiled.keyboardAppearance = UIKeyboardAppearanceDark;
    
    return textFiled;
}


- (void)clickLoginButton{
    
    [self.accountTextFiled resignFirstResponder];
    [self.passwordTextFiled resignFirstResponder];
    
    if ([self.accountTextFiled.text isEqualToString:@""] || [self.passwordTextFiled.text isEqualToString:@""]) {
        
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                       message:@"请完善注册信息！"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction * action) {
                                                                  //响应事件
                                                                  
                                                              }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        SHOWHUD;
        
        NSDictionary * parametersDic = @{@"mobile":self.accountTextFiled.text,@"password":self.passwordTextFiled.text};
        
        [[NetworkTool sharedTools] POST:@"/api/user/reg" parameters:parametersDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
            
            HIDDENHUD;
            
            //            NSLog(@"res===%@",responseObject);
            
            NSString * codeString = [NSString stringWithFormat:@"%@",responseObject[@"status"]];
            
            if ([codeString isEqualToString:@"200"]) {
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:@"恭喜你注册成功！赶快去登录吧！"
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                          [self.navigationController popViewControllerAnimated:YES];
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }else{
                
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@""
                                                                               message:responseObject[@"msg"]
                                                                        preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel
                                                                      handler:^(UIAlertAction * action) {
                                                                          
                                                                      }];
                
                [alert addAction:defaultAction];
                [self presentViewController:alert animated:YES completion:nil];
                
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            HIDDENHUD;
            
            NSLog(@"error===%@",error);
        }];
        
    }
}


- (void)clickVerifyButton{
    
    
}


-(void)clickBackButton{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
